'use strict';

const Controller = require('egg').Controller;

class OrderController extends Controller {
    // 列表
    async index(){
        const { ctx,app } = this;

        let data = await ctx.page('Order',{},{
            include:[{
                model:app.model.Class
            }]
        })

        data = JSON.parse(JSON.stringify(data))

        await ctx.renderTemplate({
            title:"成绩单列表",
            tempType: "table",
            table:{
                // 按钮
                buttons: {
                    // 新增操作
                    add: "/admin/order/create"
                },
                // 表头
                columns:[{
                    title:"ID",
                    fixed: 'center',
                    key:"id"
                },{
                    title:"考试名称",
                    fixed: 'left',
                    key:"name"
                },{
                    title:"年级班级",
                    fixed: 'left',
                    render(item) {
                        return `
                        <h2 class="table-avatar">
                            ${item.class.name}
                        </h2>
                        `
                    }
                },{
                    title:"成绩单状态",
                    fixed: 'center',
                    render(item){
                        const o = [
                            {
                                text:"未录入",
                                color:"warning"
                            },
                            {
                                text:"已完成",
                                color:"success"
                            },
                            {
                                text:"已录入",
                                color:"danger"
                            }
                ]
                        let v = o[item.status]
                        return `
                        <span class="badge badge-${v.color}">${v.text}</span>
                        `
                    }
                },{
                    title:"创建时间",
                    fixed: 'center',
                    width: 180,
                    key:"created_time"
                },{
                    title: "操作",
                    width: 200,
                    fixed: 'center',
                    action:{
                        list: function (id) {
                            return `/admin/score?order_id=${id}`
                        },
                        enter:function(id){
                            return `/admin/score/create/${id}`
                        },
                        edit:function(id){
                            return `/admin/order/edit/${id}`
                        },
                        delete:function(id){
                            return `/admin/order/delete/${id}`
                        },
                    }
                }]
            },
            data
        })
    }

    // 创建成绩单表单
    async create(){
        const { ctx,app } = this;
        let classo = await app.model.Class.findAll()
        classo = JSON.parse(JSON.stringify(classo))
        console.log(classo)
        await ctx.renderTemplate({
            // 页面标题
            title: "添加成绩单子",
            // 模板类型 form表单，table表格分页
            tempType: "form",
            // 表单配置
            form: {
                // 提交地址
                action: "/admin/order",
                // 字段配置
                fields:[{
                    label: "考试名称",
                    type: "text",
                    name: "name",
                    placeholder: "考试名称"
                },{
                    label: "所属班级",
                    type: "select",
                    name: "class_id",
                    field:"name",
                    options:classo,
                    placeholder: "所属班级"
                }]
            },
            // 新增成功跳转路径
            successUrl:"/admin/order"
        });
    }

    // 创建成绩单逻辑
    async save(){
        const { ctx,app } = this;

        ctx.validate({
            name:{
                type:"string",
                required:true,
                desc:"考试名称"
            },
            class_id:{
                type:"int",
                required:true,
                desc:"所属班级"
            },
        })

        let { name,class_id } = ctx.request.body;

        if(await app.model.Order.findOne({
            where:{
                name
            }
        })){
            return ctx.apiFail('该考试已存在');
        }

        let order = await app.model.Order.create({
            name,class_id
        })

        ctx.apiSuccess(order)
    }

    // 编辑表单页
    async edit(){
        const { ctx,app } = this
        let classo = await app.model.Class.findAll()
        classo = JSON.parse(JSON.stringify(classo))
        const id = ctx.params.id

        let data = await app.model.Order.findByPk(id);
        if(!data){
            return await ctx.pageFail('该记录不存在')
        }

        data = JSON.parse(JSON.stringify(data))
        console.log(data)

        await ctx.renderTemplate({
            id,
            title:"修改成绩单",
            tempType:"form",
            form:{
                // 提交地址
                action: "/admin/order/" + id,
                // 字段配置
                fields:[{
                    label: "考试名称",
                    type: "text",
                    name: "name",
                    placeholder: "考试名称"
                },{
                    label: "所属班级",
                    type: "select",
                    name: "class_id",
                    field:"name",
                    options:classo,
                    placeholder: "所属班级"
                }],
                // 默认值
                data
            },
            // 修改成功跳转路径
            successUrl:"/admin/order"
        })
    }

    // 更新逻辑
    async update(){
        const { ctx,app } = this;
        ctx.validate({
            id:{
                type:"int",
                required:true
            },
            name:{
                type:"string",
                required:true,
                desc:"考试名称"
            },
            class_id:{
                type:"int",
                required:true,
                desc:"所属班级"
            },
        })

        let id = ctx.params.id
        let { name,class_id } = ctx.request.body

        let order = await app.model.Order.findOne({
            where:{
                id
            }
        })
        if(!order){
            return ctx.apiFail('该记录不存在')
        }

        const Op = app.Sequelize.Op

        if((await app.model.Order.findOne({
            where:{
                id:{
                    [Op.ne]:id
                },
                name
            }
        }))){
            return ctx.apiFail('该成绩单已存在')
        }

        order.name = name
        order.class_id = class_id

        ctx.apiSuccess(await order.save())

    }

    // 删除
    async delete(){
        const { ctx,app } = this;
        const id = ctx.params.id
        await app.model.Order.destroy({
            where:{
                id
            }
        })
        ctx.toast('删除成功','success')
        ctx.redirect('/admin/order')
    }
}


module.exports = OrderController;
